package com.shanzmoo.core.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.core.domain.DeliveryBillCreator;
import com.shanzmoo.db.logistics.entity.DeliveryBillEntity;
import com.shanzmoo.db.logistics.entity.DeliveryLogisticsDetailEntity;
import com.shanzmoo.db.logistics.entity.DeliverySkuDetailEntity;
import com.shanzmoo.db.logistics.entity.query.DeliveryBillPageQuery;
import com.shanzmoo.db.logistics.entity.vo.DeliveryBillInfoVO;
import com.shanzmoo.db.logistics.entity.vo.DeliveryBillPageVO;
import com.shanzmoo.db.logistics.entity.vo.DeliveryLogisticsDetailVO;
import com.shanzmoo.db.logistics.entity.vo.DeliverySkuDetailVO;
import com.shanzmoo.db.logistics.service.IDeliveryBillService;
import com.shanzmoo.db.logistics.service.IDeliveryLogisticsDetailService;
import com.shanzmoo.db.logistics.service.IDeliverySkuDetailService;
import com.shanzmoo.db.order.entity.OutletOrderDetailEntity;
import com.shanzmoo.db.order.entity.OutletOrderEntity;
import com.shanzmoo.db.order.entity.dto.LogisticsDeliverDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 发货管理
 *
 * @author： Gzhao 2021/1/31
 */
@Slf4j
@Component
public class DeliveryManager {

    @Autowired
    private IDeliveryBillService deliveryBillService;
    @Autowired
    private IDeliveryLogisticsDetailService deliveryLogisticsDetailService;
    @Autowired
    private IDeliverySkuDetailService deliverySkuDetailService;

    @Transactional(rollbackFor = Exception.class)
    public void generateSlefBill(OutletOrderEntity order, List<OutletOrderDetailEntity> detailList, int orderType) {
        DeliveryBillEntity deliveryBillEntity = DeliveryBillCreator.createBill(order.getOrderNo(), orderType);
        deliveryBillEntity.setOutletId(CurrentUserCache.getOutletId());
        deliveryBillService.save(deliveryBillEntity);

        // 发货单物流明细
        List<DeliveryLogisticsDetailEntity> logisticsDetailList = DeliveryBillCreator.createNoLogisticsDetailList(deliveryBillEntity.getId());
        deliveryLogisticsDetailService.saveBatch(logisticsDetailList);

        // 发货单商品明细
        List<DeliverySkuDetailEntity> skuDetailList = DeliveryBillCreator.createSkuDetailList(detailList, deliveryBillEntity.getId());
        deliverySkuDetailService.saveBatch(skuDetailList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void generateLogisticsBill(LogisticsDeliverDto deliverDto, int orderType) {
        DeliveryBillEntity deliveryBillEntity = DeliveryBillCreator.createBill(deliverDto.getOrderNo(), orderType);
        deliveryBillEntity.setOutletId(CurrentUserCache.getOutletId());
        deliveryBillService.save(deliveryBillEntity);

        // 发货单物流明细
        List<DeliveryLogisticsDetailEntity> logisticsDetailList = DeliveryBillCreator.createLogisticsDetailList(
                deliveryBillEntity.getId(), deliverDto.getLogisticsDetailList());
        deliveryLogisticsDetailService.saveBatch(logisticsDetailList);

        // 发货单商品明细
        List<DeliverySkuDetailEntity> skuDetailList = DeliveryBillCreator.createSkuDetailList2(deliverDto.getSkuDetailList(), deliveryBillEntity.getId());
        deliverySkuDetailService.saveBatch(skuDetailList);

    }

    public SResponse<PageRespBase<DeliveryBillPageVO>> getPageList(DeliveryBillPageQuery query) {
        Page page = PageHelper.startPage(query.getPageNum(), query.getPageSize());

        if (CurrentUserCache.getOutletId() >  0) {
            query.setOutletId(CurrentUserCache.getOutletId());
        }

        List<DeliveryBillPageVO> reList = deliveryBillService.queryList(query);

        PageRespBase<DeliveryBillPageVO> re = PageRespBase.create(page, reList);
        PageHelper.clearPage();

        return SResponse.ok(re);
    }

    public SResponse<DeliveryBillInfoVO> getDatails(Integer id) {
        List<DeliveryLogisticsDetailEntity> logisticsDetailEntityList = deliveryLogisticsDetailService.list(
                new QueryWrapper<DeliveryLogisticsDetailEntity>()
                        .eq("delivery_id", id)
        );

        List<DeliveryLogisticsDetailVO> logisticsDetailList = logisticsDetailEntityList.stream()
                .map(DeliveryLogisticsDetailVO::create)
                .collect(Collectors.toList());


        List<DeliverySkuDetailEntity> skuDetailEntityList = deliverySkuDetailService.list(
                new QueryWrapper<DeliverySkuDetailEntity>()
                        .eq("delivery_id", id)
        );

        List<DeliverySkuDetailVO> skuDetailList = skuDetailEntityList.stream()
                .map(DeliverySkuDetailVO::create)
                .collect(Collectors.toList());


        DeliveryBillInfoVO billInfo = DeliveryBillInfoVO.create(logisticsDetailList, skuDetailList);

        return SResponse.ok(billInfo);
    }
}
